package com.sanbi.shield.server.repository.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanbi.shield.server.common.bean.bo.UserExtraInfoBO;
import com.sanbi.shield.server.common.bean.bo.UserInfoBO;
import com.sanbi.shield.server.jdbc.domain.UserInfo;
import com.sanbi.shield.server.jdbc.mapper.UserInfoMapper;
import com.sanbi.shield.server.jdbc.query.UserInfoQuery;
import com.sanbi.shield.server.repository.mapping.UserInfoMapping;
import com.sanbi.shield.server.repository.service.UserInfoRepository;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author system
 * @since 2024/07/25
 */
@Repository
public class UserInfoRepositoryImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoRepository {
    /**
     * 用户信息Mapping
     */
    @Resource
    private UserInfoMapping userInfoMapping;

    @Override
    public UserInfoBO queryByLoginName(String loginName){
        if(StringUtils.hasText(loginName)){
            LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserInfo::getLoginName, loginName);
            UserInfo record = this.getOne(queryWrapper);
            if(null == record){
                return null;
            }
            UserInfoBO bo = userInfoMapping.toBO(record);
            return bo.setExtraInfo(StringUtils.hasText(record.getExtraInfo())
                    ? JSON.parseObject(record.getExtraInfo(), UserExtraInfoBO.class) : new UserExtraInfoBO());
        }
        return null;
    }

    @Override
    public UserInfoBO queryById(Long id) {
        if(null == id){
            return null;
        }
        UserInfo record = this.getById(id);
        UserInfoBO bo = userInfoMapping.toBO(record);
        return bo.setExtraInfo(StringUtils.hasText(record.getExtraInfo())
                ? JSON.parseObject(record.getExtraInfo(), UserExtraInfoBO.class) : new UserExtraInfoBO());
    }

    @Override
    public List<UserInfoBO> queryById(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        List<UserInfo> records = this.listByIds(ids);
        return records.stream().map(e -> {
            UserInfoBO bo = userInfoMapping.toBO(e);
            return bo.setExtraInfo(StringUtils.hasText(e.getExtraInfo())
                    ? JSON.parseObject(e.getExtraInfo(), UserExtraInfoBO.class) : new UserExtraInfoBO());
        }).collect(Collectors.toList());
    }

    @Override
    public List<UserInfoBO> queryList(UserInfoQuery query) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(query.getRealName()), UserInfo::getRealName, query.getRealName());
        List<UserInfo> records = this.list(queryWrapper);
        return records.stream().map(e -> {
            UserInfoBO bo = userInfoMapping.toBO(e);
            return bo.setExtraInfo(StringUtils.hasText(e.getExtraInfo())
                    ? JSON.parseObject(e.getExtraInfo(), UserExtraInfoBO.class) : new UserExtraInfoBO());
        }).collect(Collectors.toList());
    }

}
